I have only recently started collecting the Interview Questions for Core Python. I will keep on adding more interview questions.
I am also planning to pen a seperate ebook with details questions and solutions of them under the name of FAQ's: Core Python. It will be similar to one which I created for MSI Package & Repackaging (FAQ's:MSI Packaging & Repackaging").
Python execution flow_val__val_1val1_valval1val1_ifval_1val 1x-y-zxyx_ZXYZ_abcinlocal variable names should not begin with an underscore discouraged
In [ ]:
_ variableid returns
In [ ]:
infWhat is the output of the followings (using Python 3.x)
float(‘nan’)
print(round(2.5) - round(-10.5))
print(round(0.50000000000001))
print(0.3/(0.1 + 0.2))
What is the output of the followings (using Python 2)
float(‘nan’)
print(round(2.5) - round(-10.5))
print(round(0.50000000000001))
~*['Roshan Musheer'] * 4
print("Hello, What are you doing', \"This is good")
print('Hello, What are you doing\', \"This is good\"')
a = " "
b = "Rama"
"Sri".join(a).join(b)
a = " "
b = "Rama"
"Sri".join(b).join(a)
a = "Sri"
b = "Rama"
b.join(a).join(" ")
a = "Sri"
b = "Rama"
b.join(" ").join(a)
name = "Rigveda"
name[3]="V"
In [ ]:
list and tuplelistlistlistdel and remove methods of list same
In [23]:
lst = [1, 2, 3, 44, 4, 2, 44, 55, 2, 34]
print(list(set(lst)))
In [25]:
lst = [1, 2, 3, 44, 4, 2, 44, 55, 2, 34]
lst.count(2)
Out[25]:
In [26]:
# not using `re` as it has not be taught.
gita_txt = """The Supreme Lord is situated in everyone's heart, O Arjuna,
and is directing the wanderings of all living entities,
who are seated as on a machine, made of the material energy."""
sorted_txt = sorted(set(gita_txt.replace(',',' ').replace('\t',' ').replace('\n',' ').split(" ")))
print(sorted_txt)
In [27]:
# Check the `''` in the previous solution, we need to remove it.
gita_txt = """The Supreme Lord is situated in everyone's heart, O Arjuna,
and is directing the wanderings of all living entities,
who are seated as on a machine, made of the material energy."""
sorted_txt = sorted(set(gita_txt.replace(",", ' ').replace('\n',' ').split(" ")))
sorted_txt.remove("")
print(sorted_txt)
In [41]:
# using re module
# https://stackoverflow.com/questions/1059559/split-strings-with-multiple-delimiters
import re
s_list = sorted(set(re.split("[, \-!?:\n]+", gita_txt)))
print(s_list)
```python
def listing(num, lst=[]):
lst.append(num)
return True
l = [1, 2, 3]
b = listing(10, l)
print(b, l)
```
-
```python
def listing(num, lst):
lst.append(num)
return True
l = [1, 2, 3]
b = listing(10, l)
print(b, l)
```
-
```python
def listing(num, lst=[]):
lst.append(num)
return True
l = [1, 2, 3]
b = listing([10], l)
print(b, l)
```
-
```python
def listing(num, lst=[]):
lst.append(num)
return True
l = [1, 2, 3]
b = listing([10], l)
print(b, l)
```
-
```python
101 in [101, 102, 103]
```
-
```python
104 in [101, 102, 103]
```
- ```python
l = ["Mango", 'is', 'a', 'fruit']
" ".join(l)
```
dictionary as tuple of list
In [28]:
# using open command to read the text file and store the value in `v_txt` variable
v_txt = """O nourisher, and enlightened person the policy which urges upon others the attainment of knowledge and
wealth is like a saw, that uphold the heart of people like you and spread good virtues far and near.
O teachers and preachers! you uphold kingdom or wealth every day (by your noble teachings), by the simile of illustration of the sun, you make firm the summit or the
advancement of the State, by whose association a man who is illuminator or instructor of all objects becomes firm and not decayinghaving
reached the earth and desirable knowledge, is increaser of the life, those who approach such a man and those (teachers and
preachers) ever enjoy happiness.
O men ! always have association with those teachers and preachers, who illuminate the dealirg of knowledge like the sun and increase kingdom, wealth and span of life and uphold
(establish) all in happiness. It is they by whose association, men become endowed with knowledge.
"""
words = v_txt.split()
# Lets create the dictionary with words, all the values has been defaulted to 0
v_dict = {}.fromkeys(words,0)
print(v_dict)
print("*"*20)
for w in words:
v_dict[w] += 1
print(v_dict)
In [31]:
my_dict = {"a" : 4, "b": 10, "12": 3, "d": 33, "1": 22}
for a in sorted(my_dict):
print(a)
p = "Manish"
print(p[0:2])
print(p[:2])
print(p[1:4])
print(p[-5:-2])
print (p[::-1])
print("implemented"[::-2])
print("implemented"[-5::-2])
print("implemented"[:-7:-2])
print("implemented"[::2])
print("implemented"[1::2])
print("implemented"[1:4:2])
x = "Mayank Johri"
print(x)
print("id(x)",id(x))
print("x[0:]", id(x[0:]))
print(id(x[1:]))
print(id(x[2:]))
print(x[0:])
print(x[1:])
print(x[2])
continue and breakwhile-else be executedname = "Sunil Kumar Bhele"
lst = ["Sunil Kumar Bhele", "Rajeev", "Sachin", "Dhoke"]
result = "DMS Alumni" if name in lst else "Not"
print(result)
name = "Sunil Kumar Bhele"
lst = ["Sunil Kumar Bhele", "Rajeev", "Sachin", "Dhoke"]
if name in lst: print("DMS Alumni")
In [38]:
global keywordwhat is the output of the follows:
def test():
pass
d = test()
print(d)
def test():
return "TEST", "Testing", True
print(test())
def test(txt, t):
return (txt + '1') * t
print(test("TEST", 2))
__ init__ function is used forGlobal variables be shared across modulestry-except-else be executedwith statementprint(0.1 + 0.11 == 0.12)
print(0.11 + 0.2 == 0.31)
print(~1011)
a = 10
a = b
5 + 2 //4
5 + 2%4
5 + 2%4
5 + 2*4
5 % 2*4 // 2
int(5.55 % 2+3/3)
round(5.55 % 2+3/3)
try: except: raise, finally.
In [ ]: